{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>博物馆参观指南</title>
    <!-- plugins:css -->
    <link rel="shortcut icon" href="{% static '/images/logo.svg' %}" type="image/x-icon">
    <link rel="stylesheet" href="{% static 'vendors/mdi/css/materialdesignicons.min.css' %}">
    <link rel="stylesheet" href="{% static 'vendors/base/vendor.bundle.base.css' %}">
    <!-- endinject -->
    <!-- plugin css for this page -->
    <link rel="stylesheet" href="{% static 'vendors/datatables.net-bs4/dataTables.bootstrap4.css' %}">
    <!-- End plugin css for this page -->
    <!-- inject:css -->
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <!-- endinject -->
    <script type="text/javascript" src="{% static 'js/echarts.min.js' %}"></script>
    <script src="{% static 'js/jquery-3.6.0.min.js' %}"></script>
</head>

<body>
  <div class="container-scroller">
    <div class="container-fluid page-body-wrapper full-page-wrapper">
      <div class="content-wrapper d-flex align-items-stretch auth auth-img-bg">
        <div class="row flex-grow">
          <div class="col-lg-6 d-flex align-items-center justify-content-center">
            <div class="auth-form-transparent text-left p-3">
              <div class="brand-logo">
                <img src="{% static 'images/logo.svg' %}" alt="logo">
              </div>
              <h4>用户登录?</h4>
              <h6 class="font-weight-light">Hello! let's get started</h6>
<form id="smsForm" class="pt-3" method="post" novalidate>
                  {% csrf_token %}

            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                            <div class="input-group">

                                {{ field }}


                                <div class="input-group-append">

                                                    <input value="点击获取验证码" type="button" id="btnSms"
                                                           class="btn btn-sm btn-primary">
                                                </div>

                            </div>
                        <span class="text-danger">{{ field.errors.0 }}</span>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="text-danger"></span>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-lg-12">
<input id="btnSubmit" type="button" class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn" value="登  录"/>                </div>
            </div>
               <div class="text-center mt-4 font-weight-light">
                  <a href="{% url 'login' %}" class="text-primary">   用户名密码登录 &nbsp;&nbsp;&nbsp;</a>
             <a href="{% url 'register' %}" class="text-primary">没有账号，去注册</a>
                </div>

        </form>

            </div>
          </div>
          <div class="col-lg-6 register-half-bg d-flex flex-row">
            <p class="text-white font-weight-medium text-center flex-grow align-self-end">&copy; Museum Guide.</p>
          </div>
        </div>
      </div>
      <!-- content-wrapper ends -->
    </div>
    <!-- page-body-wrapper ends -->
  </div>
  <!-- container-scroller -->
  <!-- plugins:js -->
  <script src="{% static 'vendors/base/vendor.bundle.base.js' %}"></script>
  <script src="{% static 'js/hoverable-collapse.js' %}"></script>
  <script src="{% static 'js/template.js' %}"></script>
  <script src="{% static 'js/off-canvas.js' %}"></script>
  <style>
      .auth .register-half-bg {
    background: url({% static 'images/故宫.jpeg' %});
    background-size: cover;
}
  </style>
  <script>
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        })

        /*
            点击验证码发送短信
         */
        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty()
                // 获取用户的手机号
                var mobile_phone = $('#id_mobile_phone').val();

                $.ajax({
                    url: "{% url 'send_sms'%}",
                    type: "GET",
                    data: {mobile_phone: mobile_phone, tpl: 'login'},
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            // 启动倒计时功能
                            sendSmsRemind();
                        } else {
                            // 错误 显示对应的错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            });
                        }
                    }
                })
            })
        }

        /*
            倒计时功能
         */
        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');
            // 禁用状态
            $smsBtn.prop('disabled', true)
            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time + '秒重新发送');
                time -= 1;
                if (time < 1) {
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)
        }

        /*
            实现登录
         */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: '{% url 'login_sms' %}',
                    type: 'POST',
                    data: $('#smsForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            // 跳转到登录页面
                            location.href = res.data
                        } else {
                            // 错误 显示对应的错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            });
                        }
                    }
                });
            });
        }

    </script>

  <!-- endinject -->
</body>

</html>
